package com.rxt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rxt.common.result.Result;
import com.rxt.model.domain.PBuy;
import com.rxt.model.dto.UUserDto;
import com.rxt.service.IPBuyService;
import com.rxt.util.BaseContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Api(tags = "采购模块")
@Slf4j
@RestController
@RequestMapping("/pBuy")
public class PBuyController {

    @Autowired
    private IPBuyService ipBuyService;

    @ApiOperation("增加采购信息")
    @PostMapping
    public Result save(@RequestBody PBuy pBuy){
        log.info("新增采购信息，采购信息为：{}",pBuy.toString());

        //获得UserDto对象
        UUserDto user = BaseContext.get();
        //获得用户名id、用户名
        Long id = user.getId();
        String username = user.getUsername();

        pBuy.setUserId(id);
        pBuy.setUser(username);

        //更新发布时间
        pBuy.setPublishTime(LocalDateTime.now());

        ipBuyService.save(pBuy);

        return Result.ok();
    }


    @ApiOperation("采购分页查询")
    @GetMapping("/page")
    public Result page(int page,int pageSize,String name){
        log.info("采购分页查询，参数为：page = {}，pageSize = {}，name = {}",page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<PBuy> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),PBuy::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(PBuy::getPublishTime);

        //执行查询
        ipBuyService.page(pageInfo,queryWrapper);

        return Result.ok(pageInfo);
    }

    @ApiOperation("删除采购信息")
    @DeleteMapping
    public Result delete(Long id){
        log.info("删除采购信息，id为{}",id);

        ipBuyService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("获取近日采购查询")
    @GetMapping("/recent")
    public Result getRecentPurchase(){
        List<PBuy> buys = ipBuyService.list(new LambdaQueryWrapper<PBuy>()
                .eq(PBuy::getDeleteStatus, 0)
                .orderByDesc(PBuy::getPublishTime)
                .last("limit 10"));
        return Result.ok(buys);
    }

    @ApiOperation("获取个人采购查询")
    @GetMapping("/persons")
    public Result getPsersons(){
        List<PBuy> list = ipBuyService.list(new LambdaQueryWrapper<PBuy>().eq(PBuy::getUserId, BaseContext.get().getId()));
        return Result.ok(list);
    }

    @ApiOperation("获取采购详情信息")
    @GetMapping("/{id}")
    public Result getPurchaseDetail(@PathVariable Long id) {
        PBuy buy = ipBuyService.getById(id);
        return Result.ok(buy);
    }

}

